<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('User_model');
		$this->load->library('form_validation');
	}		

	public function index()
	{
		/*
		$this->load->database('');
		$data = array(
				'userid' => 'test2',
				'uname'	 => '无敌铁金刚',
				'pwd'	 => md5('123456'),
				'email'	 => 'test1@163.com',
				'sex'	 => '男',
				'jointime' => time()-1000,
				'joinip'   => '192.168.1.175',
				'logintime'=> time(),
				'loginip'  => '113.31.82.168'
			);
		$this->db->insert('youley_member', $data);
		echo $this->db->affected_rows();
		 */
		/*
		$sql = "INSERT INTO app_youley.youley_member (userid, uname, pwd, email, sex, jointime, joinip, logintime, loginip) VALUES ('test1', '无敌小金刚', '" . md5('123456') . "', 'test1@163.com', '男', '". time() ."', '192.168.1.175', '". time() ."', '113.31.182.168')";
		$this->db->query($sql);
		echo $this->db->affected_rows();
		 */
		if (!$this->User_model->IsLogin())
		{
			$this->load->view('user_index.htm');
		}
		else
		{
			echo 'mid='.$this->User_model->mid;
			echo '<br><a href="/user/delcookie">删除cookie</a>';
		}
	}

	public function test_wang()
	{
		//$this->output->cache(5);
		$data['title'] = 'test_wang';
		$data['message'] = '这里使用了.htm格式的模板，字符编码为utf-8';
		$this->load->view('test_wang.htm', $data);
	}

	public function addcookie()
	{
		$this->User_model->Add_test_cookie(10);
		header("Location: /user");
	}

	public function delcookie()
	{
		$this->User_model->Clear_cookie();
		header("Location: /user");
	}

	public function login()
	{
		$this->form_validation->set_rules('userid','userid','required|min_length[4]|max_length[12]|alpha_dash');
		if ($this->form_validation->run() == FALSE)
		{
			//echo '有错误！';
			/*
			$this->form_validation->set_message('required', 'error:required');
			$this->form_validation->set_message('min_length', 'error:min');
			$this->form_validation->set_message('max_length', 'error:max');
			$this->form_validation->set_message('alpha_dash', 'error:alpha');
			 */
			$data['title'] = '用户登录';
			$this->load->view('user_index.htm', $data);
		}
		$this->userid = $this->input->post('userid', TRUE);
		$this->pwd = $this->input->post('pwd', TRUE);
		//$query = $this->db->get('youley_member');
		$query = $this->db->get_where('youley_member', array('userid'=>$this->userid));
		//print_r($query->result());
	}

	public function regnew()
	{
		
	}

	/**
	 * 生成验证码(只是记录试验代码，并非实例)
	 */ 
	public function createCaptcha()
	{
			$this->load->helper('captcha');
			$data['title'] = '用户登录';

			$vals = array(
					'word' => '',
					'img_path' => './captcha/',
					'img_url' => 'http://youley/captcha/',
					'font_path' => '',
					'img_width' => '90',
					'img_height' => '30',
					'expiration' => '7200'
			);
			$cap = create_captcha($vals);
			$data['captcha'] = $cap['image'];
			$this->load->view('login.htm', $data);
	
	}

}

/* End of file user.php */
